<template>
  <div class="home">
    <div>
      <el-date-picker
        v-model="startTime"
        type="datetime"
        :picker-options="{
          disabledDate: (time) => {
            return dataTime.startTimeData(time)
          },
          selectableRange: this.startTimeRange,
        }"
        value-format="yyyy-MM-dd HH:mm:ss"
        format="yyyy-MM-dd HH:mm:ss"
        placeholder="开始时间"
      ></el-date-picker>
    </div>
    <div>
      <el-date-picker
        v-model="endTime"
        type="datetime"
        :picker-options="{
          disabledDate: (time) => {
            return dataTime.endTimeData(time)
          },
          selectableRange: this.endTimeRange,
        }"
        value-format="yyyy-MM-dd HH:mm:ss"
        format="yyyy-MM-dd HH:mm:ss"
        placeholder="结束时间"
      ></el-date-picker>
    </div>
    <div class="block">
      <span class="demonstration">默认</span>
      <el-date-picker
        v-model="value1"
        size="mini"
        type="datetimerange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期">
      </el-date-picker>
    </div>
  </div>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      startTime: "",
      endTime: "",
      startTimeRange: null,
      endTimeRange: null,
      dataTime: {
        // 开始时间范围限制
        startTimeData: (time) => {
          if (this.endTime != "" && this.startTime == "") {
            return time.getTime() < Date.now() || time.getTime() < this.endTime;
          } else {
            return time.getTime() < Date.now() - 8.64e7;
          }
        },
        // 结束时间范围限制
        endTimeData: (time) => {
          return (
            time.getTime() < this.startTime ||
            time.getTime() < Date.now() - 8.64e7
          );
        },
      },
      value1: [new Date(2000, 10, 10, 10, 10), new Date(2000, 10, 11, 10, 10)],
    };
  },
  watch: {
    startTime: {
      handler(newValue) {
        if (newValue) {
          let nowDate = this.format(new Date(), "-");
          let dt = nowDate.split(" ");
          let st = "";
          if (newValue.split(" ")[0] == dt[0]) {
            // 是今天,选择 的时间开始为此刻的时分秒
            st = dt[1];
          } else {
            // 明天及以后从0时开始
            st = "00:00:00";
          }
          this.startTimeRange = st + " - 23:59:59";
          //例如：如果今天此刻时间为10:41:40 则选择时间范围为： 10:41:40 - 23:59:59
          //否则为：00:00:00- 23:59:59
        }
      },
    },
    endTime: {
      handler(newValue) {
        if (newValue) {
          let zt = this.startTime.split(" "); //开始日期
          let st = "";
          //如果开始日期和结束日期都选择当天
          if (newValue.split(" ")[0] == zt[0]) {
            st = zt[1];
          } else {
            st = "00:00:00";
          }
          this.endTimeRange = st + " - 23:59:59";
        }
      },
    },
  },
  methods: {
    // 时间补0
    supplement(nn) {
      return (nn = nn < 10 ? "0" + nn : nn);
    },
    // 转换时间格式
    format(Date, str) {
      var obj = {
        Y: Date.getFullYear(),
        M: Date.getMonth() + 1,
        D: Date.getDate(),
        H: Date.getHours(),
        Mi: Date.getMinutes(),
        S: Date.getSeconds(),
      };
      // 拼接时间 hh:mm:ss
      var time =
        " " +
        this.supplement(obj.H) +
        ":" +
        this.supplement(obj.Mi) +
        ":" +
        this.supplement(obj.S);
      // yyyy-mm-dd
      if (str.indexOf("-") > -1) {
        return (
          obj.Y +
          "-" +
          this.supplement(obj.M) +
          "-" +
          this.supplement(obj.D) +
          time
        );
      }
      // yyyy/mm/dd
      if (str.indexOf("/") > -1) {
        return (
          obj.Y +
          "/" +
          this.supplement(obj.M) +
          "/" +
          this.supplement(obj.D) +
          time
        );
      }
    },
  },
};
</script>